package sdkshell.kuaiyan.com.kuaiyansdk.http;

import android.content.Context;

import com.wmcsk.dl.util.LogUtils;

import java.util.HashMap;
import java.util.Map;

import sdkshell.kuaiyan.com.kuaiyansdk.http.Ihttp.NetBuilderConfig;
import sdkshell.kuaiyan.com.kuaiyansdk.http.callback.HttpCallBack;
import sdkshell.kuaiyan.com.kuaiyansdk.http.enums.RequestType;
import sdkshell.kuaiyan.com.kuaiyansdk.http.httpIpm.NetBuildConfigImp;

/**
 * Created by Max on 2016/6/23.
 */
public class NetHttp implements Net {
    private NetBuilderConfig config;
    public NetHttp(Context context){
        NetBuilderConfig config = new NetBuildConfigImp(context) {
            @Override
            public Net build() {
                return NetHttp.this;
            }
        };
        this.config = config;
    }

    public static NetHttp getNewInstance(Context context){
        NetHttp okNetHttp = new NetHttp(context);
        return okNetHttp;
    }

    @Override
    public Net init(NetBuilderConfig config) {
        if(config == null){
            LogUtils.getNewInstance(this).d("config will not changed and use default if config == null");
            return this;
        }
        this.config = config;
        return this;
    }
    @Override
    public NetBuilderConfig getConfig() {
        return config;
    }

    @Override
    public Net setUrl(String url) {
        config.getNetConfig().setUrl(url);
        return this;
    }

    @Override
    public Net add(String key, String value) {
        params.put(key, value);
        config.getNetConfig().addParams(key,value);
        return this;
    }
    @Override
    public Net add(Map<String, String> map) {
        this.params.putAll(map);
        config.getNetConfig().addParamsAll(map);
        return this;
    }

    @Override
    public Net setRequestType(RequestType type) {
        this.type = type;
        config.getNetConfig().setType(type);
        return this;
    }

    @Override
    public void requestNet_info(HttpCallBack andyCallBack) {
        NumCountHttp.requestNetInfo(config,type, params, andyCallBack);
    }

    @Override
    public void download(HttpCallBack andyCallBack) {

    }

    @Override
    public void upload(HttpCallBack andyCallBack) {

    }

    @Override
    public void releaseConnection() {

    }

    private RequestType type = RequestType.GET;
    private Map<String, String> params = new HashMap<>();

}
